// ActionScript file
package com.nstar.base.util
{
	/**
	 * 效果工具
	 * @author: 朱传明
	 */
   
    import com.adobe.ac.mxeffects.CubeRotate;
    import com.adobe.ac.mxeffects.DistortionConstants;
    import com.nstar.base.effects.*;
    import com.nstar.countdown.view.components.CountDownPanelSimple;
    
    import flash.filters.BitmapFilter;
    import flash.filters.BitmapFilterQuality;
    import flash.filters.GlowFilter;
    import flash.geom.Point;
    import flash.utils.*;
    
    import mx.containers.ViewStack;
    import mx.controls.Label;
    import mx.core.Application;
    import mx.core.IFlexDisplayObject;
    import mx.effects.Fade;
    import mx.effects.Move;
    import mx.effects.Parallel;
    import mx.effects.Zoom;
    import mx.events.EffectEvent;
    import mx.managers.PopUpManager;    
    
	public class EffectUtils
	{
		private static var timedProcess:uint = uint.MAX_VALUE;
		private static const X_OFFSET = 5;
		//在指定控件上高亮显示提示信息
		public static function showMessage(effectTarget:Object,effect:HighLightSequence,
			msgOwner:Label,msg:String, color:uint=0xfff7a0):void {
			effectTarget.visible = true;	
			effect.highColor = color;
			
			if(timedProcess != uint.MAX_VALUE) {
				clearTimeout(timedProcess);
			}
			
			msgOwner.visible = true;
			msgOwner.text = msg;
			timedProcess =setTimeout(clearMessage, 5000, msgOwner,effectTarget);
			effect.play([effectTarget]);
		}
		
		private static function clearMessage(message:Object,target:Object):void {
			message.visible = false;
		}	
		
        public static function getBitmapFilter(color:Number = 0xffffff,blur:Number=8):BitmapFilter {
            var alpha:Number = 0.8;
            var blurX:Number = blur;
            var blurY:Number = blur;
            var strength:Number = 2;
            var inner:Boolean = false;
            var knockout:Boolean = false;
            var quality:Number = BitmapFilterQuality.HIGH;
            return new GlowFilter(color,
                                  alpha,
                                  blurX,
                                  blurY,
                                  strength,
                                  quality,
                                  inner,
                                  knockout);
        }	
        
        //返回发光过滤器
        public static function setFlowEffect(object:Object,color:Number = 0xffffff,blur:Number=8):void{
        	var filter:BitmapFilter = getBitmapFilter(color,blur);
        	var myFilters:Array = new Array();
        	myFilters.push(filter);
        	object.filters = myFilters;
        }
	        
		//旋转视图
		public static function rotateView(direction:String,
			target:Object,siblings:Array,duration:int=1000):void{
			var rotate:CubeRotate = new CubeRotate();
			rotate.direction = direction;
			rotate.target = target;
			rotate.siblings = siblings;
			rotate.duration = duration;
			rotate.play();
		}
		
		//旋转ViewStack中的视图
		//使用ButtonBar绑定ViewStack时，一个按钮对应一个视图，此时不需要指定第二个参数
		//如果使用上、下页两个按钮来按钮视图切换的换的话，需要明确指定一下旋转方向
		//原理：使用ViewStack来保存旧的视图的索引，以作比较，这样就能知道应该旋转的方向
		/**
		 *  //使用两个按钮切换视图时的示例代码
		 * 	//下一页
		 *	private function rotateLeftHandler(event:MouseEvent):void{
		 *		DistortionEffectsUtils.rotateViewStack(vsMain,DistortionConstants.LEFT);
		 *	}
		 * 	//上一页
		 *	private function rotateRightHandler(event:MouseEvent):void{    
		 *		DistortionEffectsUtils.rotateViewStack(vsMain,DistortionConstants.RIGHT);
		 *	}
		 *	//使用ButtonBar切换视图的代码，处理ItemClickEvent事件
		 *	function itemclickHandler(event:ItemClickEvent):void{
		 *		DistortionEffectsUtils.rotateViewStack(vsMain);
		 *	}
		 */ 
		public static function rotateViewStack(vs:ViewStack,
			direction:String=null,duration:int=1000):void{
			var oldIndex:int ;
			//获取原来的索引信息
			if (vs.data == null){
				oldIndex = 0;
			}else{
				oldIndex = int(vs.data);
			}
			
			//指定了方向时的处理，在处于最大索引后最小索引时都不使用效果
			if (direction!=null){
				switch (direction){
					case DistortionConstants.LEFT:
						//达到最大索引后不再使用效果
						if (vs.selectedIndex==(vs.getChildren().length-1)){
							return;	
						}
						vs.selectedIndex++;
						break;
					case DistortionConstants.RIGHT:
						//到达第一页时不再使用效果
						if (vs.selectedIndex == 0){
							return;
						}
						vs.selectedIndex--;
						break;	
				}
				
			}

			//如果索引相等就不使用效果
			if ((oldIndex == vs.selectedIndex) &&
			    (direction == null)){
			    	return;
			} 
			
			//保存原来的索引
			vs.data = vs.selectedIndex;
			
			var rotate:CubeRotate = new CubeRotate();
			//根据新旧索引值判断应该旋转的方向
			if (vs.selectedIndex > oldIndex ){
				rotate.direction = DistortionConstants.LEFT;
			}else{
				rotate.direction = DistortionConstants.RIGHT;
			}
			//设置旋转目标对象
			rotate.target = vs.getChildAt(oldIndex);
			rotate.siblings = [vs.getChildAt(vs.selectedIndex)];
			//播放效果
			rotate.play();
			
		}
		
		//移动并消失
		public static function moveAndDisppear(target:Object,x:Number,y:Number,
			zoomWidthTo:Number=0,zoomHeightTo:Number=0):void{
			var app:* = Application.application;
			
			var s:CountDownPanelSimple;
			var className:String = getQualifiedClassName(target);
			var classRef:Class = getDefinitionByName(className) as Class;
			var tmp:Object = new classRef();
			//tmp.data = target.data;
			
			tmp.width = target.width;
			tmp.height = target.height;
			PopUpManager.addPopUp(tmp as IFlexDisplayObject,app);
			tmp.addEventListener(EffectEvent.EFFECT_END,endHandler);
			
			//获取target在屏幕上的位置
			var p:Point = target.localToGlobal(new Point());
			
			var par:Parallel = new Parallel(tmp);
			par.duration = 1000;
			var move:Move = new Move();
			move.xFrom = p.x;
			move.yFrom = p.y;
			move.xTo = x;
			move.yTo = y;
			
			var zoom:Zoom = new Zoom();
			zoom.zoomWidthTo = zoomWidthTo;
			zoom.zoomHeightTo = zoomHeightTo;
			
			var fade:Fade = new Fade();
			fade.alphaFrom = 1;
			fade.alphaTo = 0;
			
			par.addChild(move);
			par.addChild(zoom);
			par.addChild(fade);
			
			par.play();
			
		}	        			
		
		private static function endHandler(event:EffectEvent):void{
			PopUpManager.removePopUp(event.target as IFlexDisplayObject);
		}
		
		public static function moveDownEffect(target:Object):void{
			var app:* = Application.application;
			var move:Move = new Move(target);
			move.xFrom = app.width - target.width - X_OFFSET;
			move.yFrom = -target.height-10;
			move.xTo = app.width - target.width - X_OFFSET;
			move.yTo = 0;
			move.play();
		}
		
		public static function moveUpEffect(target:Object):void{
			var app:* = Application.application;
			var move:Move = new Move(target);
			target.addEventListener(EffectEvent.EFFECT_END,endHandler);
			move.xFrom = app.width - target.width - X_OFFSET;
			move.yFrom = 0;
			move.xTo = app.width - target.width - X_OFFSET;
			move.yTo = -target.height-10;
			move.play();
		}		
	}
}